/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

interface PatternsItem {
  code: number;
  pattern: RegExp;
  message?: string;
  tag?: string;
}
const patterns = {
  phone: {
    // 手机号
    code: 3001,
    pattern: /^1[2-9]{1}[0-9]{1}\d{8}$/,
    message: '手机号码输入错误',
    tag: '手机号',
  },
  phone2: {
    // 手机号
    code: 3001,
    pattern: /^1\d{10}((([#_\-、，,转]{1})|(分机号?)|(ext?))\d{1,6})?$/i,
    message: '手机号码输入错误',
    tag: '手机号',
  },
  telPhone: {
    // 电话号码
    code: 3002,
    pattern: /^(0\d{2,3}(-|\s?))?[2-9]{1}\d{6,7}((-|\s?)\d{1,4})?$/,
    message: '电话号码输入错误',
    tag: '电话号码',
  },
  contact_blurry: {
    code: 300122,
    pattern:
      /^\d{4,11}$|^1[2-9]{1}[0-9]{1}\d{8}$|^(0\d{2,3}(-|\s?))?[2-9]{1}\d{6,7}((-|\s?)\d{1,4})?$/,
    message: '不符合指定规则',
    tag: '模糊的联系方式',
  },
  contact_4: {
    // 联系方式后四位
    code: 300121,
    pattern: /^\d{4}$/,
    message: '请输入完整手机、固话或号码后四位',
    tag: '联系方式',
  },
  contact: {
    // 联系方式
    code: 30012,
    pattern:
      /^1\d{10}([-|\+]{1}\d{3,4})?$|^(0\d{2,3}(-|\s?))?[2-9]{1}\d{6,7}((-|\s?)\d{1,4})?$|^(00)?(886)?[0][9]\d{8}$|^(852)?([6|9])\d{7}|^(853)?[6](8|6)\d{6}$/,
    msg: '请输入正确的手机或固话',
    tag: '联系方式',
  },
  decimal2: {
    // 支持小数 2位
    code: 3011,
    type: 'number',
    pattern: /^\d+(?:\.\d{1,2})?$/,
    message: '数字且最多允许两位小数',
  },
  waybillNum: {
    // 运单号
    code: 3014,
    pattern: /^[0-9a-zA-Z\-]{4,30}$/,
    message: '单号不正确',
    tag: '单号',
  },
  orderNum: {
    // 订单号
    code: 3014,
    pattern: /^[0-9a-zA-Z\-]{4,30}$/,
    message: '订单号不正确',
    tag: '订单号',
  },
  bh: {
    // 取件码
    code: 3016,
    pattern: /^\d{6}$/,
    message: '包裹编号必须为6位数字',
    tag: '包裹编号',
  },
  cardId: {
    // 身份证号码
    code: 3017,
    pattern:
      /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/,
    message: '身份证号码不合法',
    tag: '身份证号码',
  },
  fresh: {
    // 生鲜包裹
    code: 3018,
    pattern: /^生鲜[:：]/,
    message: '不是生鲜物品',
    tag: '生鲜',
  },
  webUrl: {
    code: 3019,
    pattern:
      /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i,
    message: '链接不合法',
    tag: '链接',
  },
  url: {
    code: 3021,
    pattern:
      /^(https?:\/\/)?(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i,
    message: '链接不合法',
    tag: '链接',
  },
  cn: {
    code: 3020,
    pattern: /[a-zA-Z\u4e00-\u9fa5]/gm,
    message: '无中文字符',
    tag: '中文',
  },
  password: {
    // 密码
    code: 3022,
    pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/,
    message: '密码输入错误',
    tag: '密码',
  },
};

const check = (
  key: keyof typeof patterns,
  value: string,
  special: { [k: string]: PatternsItem } = {},
) => {
  const rule = special[key] ||
    patterns[key] || {
      code: 11,
      message: `缺少对应的正则规则：${key}`,
    };
  const { pattern, message, tag } = rule;
  if (!pattern) {
    return rule;
  }
  const isExact = value.match(pattern);
  if (isExact) {
    return {
      pattern,
      tag,
      code: 0,
      message: '验证通过',
    };
  }
  return {
    code: 1001,
    pattern,
    tag,
    message: value ? message : `${tag}不能为空`,
  };
};

export { patterns as default, check };
